//
//  OrderListViewModel.m
//  xingfugangwan
//
//  Created by rainday on 15/1/2.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "OrderListViewModel.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import <AFNetworking.h>
#import "NetIP.h"

@interface OrderListViewModel(){
    NSInteger _currentPage;
    
}
@property (nonatomic, strong) NSMutableArray *dataArray;

@end



@implementation OrderListViewModel

- (id)init{

    self = [super init];
    if (self) {
        _currentPage = 1;
        _dataArray = [NSMutableArray array];
        
    }
    return self;
}


- (RACSignal *)fetchOrderDataWithStatus:(NSString *)status{

    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id token = [userDefaults objectForKey:@"token"];
        
        
        NSDictionary *postDic = [NSDictionary dictionaryWithObjectsAndKeys:status,@"status",@"1",@"current_page",@"10",@"num_per_page", nil];
        
        
        
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        [manager POST:[NSString stringWithFormat:@"%s/signing/order/",IPSTRING] parameters:postDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([[responseObject objectForKey:@"state"] isEqualToString:@"success"]) {
                
                [_dataArray addObjectsFromArray:[responseObject objectForKey:@"订单"]];
                
                [subscriber sendNext:@"success"];
            
                
                
            }else{
                [subscriber sendNext:@"failed"];

            }
            
            
            
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            
        }];
        
        
        return nil;
    }];
    
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_dataArray count]) {
        NSInteger row = indexPath.row;
        NSDictionary *dic = [_dataArray objectAtIndex:row];
        id orderId = [dic objectForKey:@"orderID"];
        id saleman = [dic objectForKey:@"负责人"];
        id lianxiren = [dic objectForKey:@"联系人"];
        return [NSString stringWithFormat:@"订单号%@  联系人:%@   负责人:%@",orderId,lianxiren,saleman];
        
    }
    
    return @"xxx";
}

- (id)getOrderIdForIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = indexPath.row;
    NSDictionary *dic = [_dataArray objectAtIndex:row];
    id orderId = [dic objectForKey:@"orderID"];
    return orderId;
    
}
@end
